package cn.gl.system.service.impl;

import cn.gl.system.domain.Role;
import cn.gl.system.mapper.RoleMapper;
import cn.gl.system.service.IRoleService;
import cn.gl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void insert(Role role) {
        super.insert(role);
        //添加当前角色的菜单
        if (role.getMenus()!=null&&role.getMenus().size()>0){
            roleMapper.addRoleMenu(role.getId(), role.getMenus());
        }
        //添加当前角色的权限
        if (role.getPermissions()!=null&&role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role.getId(), role.getPermissions());
        }
    }


    @Override
    public void delete(Long id) {
        //删除当前角色的所有菜单
        roleMapper.deletMenusByRoleId(id);
        //删除当前角色的所有权限
        roleMapper.deletePermissionsByRoleId(id);
        //删除角色和员工的中间数据
        roleMapper.deleteEmployeeBYRoleId(id);
        super.delete(id);
    }

    @Override
    public void update(Role role) {
        //删除当前角色的所有菜单
        roleMapper.deletMenusByRoleId(role.getId());
        //删除当前角色的所有权限
        roleMapper.deletePermissionsByRoleId(role.getId());
        //添加当前角色的菜单
        if (role.getMenus()!=null&&role.getMenus().size()>0){
            roleMapper.addRoleMenu(role.getId(), role.getMenus());
        }
        //添加当前角色的权限
        if (role.getPermissions()!=null&&role.getPermissions().size()>0){
            roleMapper.addRolePermissions(role.getId(), role.getPermissions());
        }

        super.update(role);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }
}
